<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;

function themeConfig($form)
{
    $logoUrl = new Typecho_Widget_Helper_Form_Element_Text('logoUrl', NULL, NULL, _t('站点LOGO地址'), _t('在这里填入一个图片URL地址, 以在网站标题前加上一个LOGO'));
    $keyswords = new Typecho_Widget_Helper_Form_Element_Text('keyswords', NULL, NULL, _t('首页关键字'), _t('首页'));
    $descriptions = new Typecho_Widget_Helper_Form_Element_Text('descriptions', NULL, NULL, _t('首页描述'), _t(''));



    $form->addInput($logoUrl);
    $form->addInput($keyswords);
    $form->addInput($descriptions);

   
}



//获取评论的锚点链接
function get_comment_at($coid)
{
    $db = Typecho_Db::get();
    $prow = $db->fetchRow($db->select('parent')->from('table.comments')
        ->where('coid = ? AND status = ?', $coid, 'approved'));
    $parent = $prow['parent'];
    if ($parent != "0") {
        $arow = $db->fetchRow($db->select('author')->from('table.comments')
            ->where('coid = ? AND status = ?', $parent, 'approved'));
        $author = $arow['author'];
        $href = '<a href="#comment-' . $parent . '">@' . $author . '</a>';
        echo $href;
    } else {
        echo '';
    }
}

//输出评论内容
function get_filtered_comment($coid)
{
    $db = Typecho_Db::get();
    $rs = $db->fetchRow($db->select('text')->from('table.comments')
        ->where('coid = ? AND status = ?', $coid, 'approved'));
    $content = $rs['text'];
    echo $content;
}

//阅读数函数
function get_post_view($archive)
{
    $cid    = $archive->cid;
    $db     = Typecho_Db::get();
    $prefix = $db->getPrefix();
    if (!array_key_exists('views', $db->fetchRow($db->select()->from('table.contents')))) {
        $db->query('ALTER TABLE `' . $prefix . 'contents` ADD `views` INT(10) DEFAULT 0;');
        echo 0;
        return;
    }
    $row = $db->fetchRow($db->select('views')->from('table.contents')->where('cid = ?', $cid));
    if ($archive->is('single')) {
        $views = Typecho_Cookie::get('extend_contents_views');
        if (empty($views)) {
            $views = array();
        } else {
            $views = explode(',', $views);
        }
        if (!in_array($cid, $views)) {
            $db->query($db->update('table.contents')->rows(array('views' => (int) $row['views'] + 1))->where('cid = ?', $cid));
            array_push($views, $cid);
            $views = implode(',', $views);
            Typecho_Cookie::set('extend_contents_views', $views); //记录查看cookie
        }
    }
    echo $row['views'];
}


// 留言加@
function getPermalinkFromCoid($coid)
{
    $db = Typecho_Db::get();
    $row = $db->fetchRow($db->select('author')->from('table.comments')->where('coid = ? AND status = ?', $coid, 'approved'));
    if (empty($row)) return '';
    return '<a href="#comment-' . $coid . '">@' . $row['author'] . '</a>';
}
//去除p标签
function p_replace($str)
{
    $newstr = preg_replace("/<p.*?>|<\/p>/is", "", $str);
    return $newstr;
}


//缩略图 获取文章第一张图 无图使用随机图
function img_postthumb($thumbThis)
{
    $db = Typecho_Db::get();
    $rs = $db->fetchRow($db->select('table.contents.text')
        ->from('table.contents')
        ->where('table.contents.cid=?', $thumbThis->cid)
        ->order('table.contents.cid', Typecho_Db::SORT_ASC)
        ->limit(1));
    preg_match_all('/\<img.*?src\=\"(.*?)\"[^>]*>/i', $rs['text'], $thumbUrl);  //通过正则式获取图片地址
    preg_match_all('/\!\[.*?\]\((http(s)?:\/\/.*?(jpg|png))/i', $rs['text'], $patternMD);  //通过正则式获取图片地址
    preg_match_all('/\[.*?\]:\s*(http(s)?:\/\/.*?(jpg|png))/i', $rs['text'], $patternMDfoot);  //通过正则式获取图片地址
    if (count($thumbUrl[0]) > 0) {
        return $thumbUrl[1][0];  //当找到一个src地址的时候，输出缩略图
    } else if (count($patternMD[0]) > 0) {
        return $patternMD[1][0];
    } else if (count($patternMDfoot[0]) > 0) {
        return $patternMDfoot[1][0];
    } else {

        return $thumbThis->widget('Widget_Options')->themeUrl . "/src/images/random/" . rand(1, 5) . ".jpg";
    }
}
